
#include "deal_input.h"



void link_create(FEIGE *feige)
{
	link_delete(feige,feige->tmp.s_addr);
	
	if(strcmp(feige->tmp.name,"zzl-virtual") != 0)
	{
		(feige->total_online_num)++;
	}
	IPMSG_USER *new = malloc(sizeof(IPMSG_USER));
	if(new == NULL)
	{
		perror("malloc");
	}
	
	printf("addr is %s\n",feige->tmp.s_addr);
	printf("name is %s\n",feige->tmp.name);
	//strcpy((char*)new ,(char*)&(feige->tmp));
	//new = &(feige->tmp);
	strcpy(new->name,feige->tmp.name);
	strcpy(new->host,feige->tmp.host);
	strcpy(new->s_addr,feige->tmp.s_addr);
	printf("addr is %s\n",new->s_addr);//feige->tmp.s_addr);

	if(feige->online_user_info_head == NULL)
	{
		feige->online_user_info_head = new;
		feige->online_user_info_head->next=NULL;
	}
	else
	{
		printf("________________it is a test_____________\n\n");
		IPMSG_USER *tmp = feige->online_user_info_head;
		while(tmp->next != NULL)
		{
			tmp = tmp->next;
		}
		tmp->next = new;
		new->next = NULL;
	}
	printf("%s is online,now online num is %d\n",feige->tmp.name,feige->total_online_num);
	
}


void link_delete(FEIGE *feige,char *offline_ipaddr)
{
	
	(feige->total_online_num)--;
	
	IPMSG_USER *tmp = feige->online_user_info_head;
	IPMSG_USER *front = feige->online_user_info_head;
	
	while(tmp != NULL)
	{
		printf("tmp:%s offline%s\n\n",tmp->s_addr,offline_ipaddr);
		if(strcmp(tmp->s_addr,offline_ipaddr) == 0)
		{
			if(tmp == feige->online_user_info_head)
			{
				feige->online_user_info_head = feige->online_user_info_head->next;
			}
			else
			{
				front->next = tmp->next;
			}
			free(tmp);
			break;
		}
		front = tmp;
		tmp = tmp->next;
	}
	printf("%s is offline,now online num is %d\n",feige->tmp.name,feige->total_online_num);
}

void link_show(FEIGE *feige)
{
	printf("___in link show___\n\n");
	IPMSG_USER *tmp = feige->online_user_info_head;
	printf("\n\n------------------show online user begins---------------\n\n");
	
	printf("\tusername\thostname\tIP_address\t\t\n");
	
	while(tmp != NULL)
	{
		printf("%s,%s,%s\n\n",tmp->name,tmp->host,tmp->s_addr);
		tmp = tmp->next;
	}
	printf("\n\n------------------show online user over---------------\n\n");
	
}